<?php 

		class Account extends CI_Controller {
		
				function __construct(){
						parent::__construct();
				}
				
				function index(){
						log_activity('test log by ' . $this->_get_current_user_name() . ' #' . $this->_get_current_user_mt4_uid(), 'user', 'user_test', 'system');

						redirect(base_url() . 'account/home/');
				}
				
				function _auth_required($type='normal',$url=''){
						// $this->_set_persistant_cookie();
						log_message('error', "[".$this->input->ip_address()."]\n".current_url().': ['.print_r($this->input->request_headers(),true).']');
				
						if( !$this->session->userdata('uid') ) {
								$token = random_string('unique');
								while($this->session->userdata($token))
								{
										$token = random_string('unique');
								}
								$url = $url ? $url : current_url();
								$this->session->set_userdata($token,$url);
								// if( !$this->input->cookie($this->config->item('sess_cookie_name')) ) {
										// $this->session->set_flashdata('sessions_login', '<p style="color:#ff0000;font-weight:bold;">Your session was logged off because your browser does not allow cookies!</p>');
								// }
								
								if($type=='ajax')
								{
										die(json_encode(array('stat'=>'not_loggedin','next_url'=>base_url() . 'sessions/login/token/'.$token)));
								}
								else
								{
										redirect(base_url() . 'sessions/login/token/'.$token);
								}
						}
				 		$user = new User();
						$user->where('id',(int)$this->session->userdata('uid'))->get(1);
						if( !$user->exists() ) {
								$token = random_string('unique');
								while($this->session->userdata($token))
								{
										$token = random_string('unique');
								}
								$url = $url ? $url : current_url();
								$this->session->set_userdata($token,$url);
								if($type=='ajax')
								{
										die(json_encode(array('stat'=>'not_loggedin','next_url'=>base_url() . 'sessions/login/token/'.$token)));
								}
								else
								{
										redirect(base_url() . 'sessions/login/token/'.$token);
								}
						}
				}
				
				function _set_persistant_cookie(){
						$cookie_name = $this->config->item('sess_cookie_name');
						if ( $this->input->cookie($cookie_name) && (!$this->input->cookie($cookie_name.'-P') || $this->input->cookie($cookie_name)!=$this->input->cookie($cookie_name.'-P'))) 
						{
								$uri = parse_url(base_url());
								$cookie = array(
										'name'   => $cookie_name.'-P',
										'value'  => $this->input->cookie($cookie_name),
										'expire' => 2678400,
										'domain' => '.' . @$uri['host'],
										'path'   => '/'
								);
								$this->input->set_cookie($cookie); 
						}
				}
				
				function _resolve_lang(){
						$langs = array('en','fr');
						$lang  = '';
						for($i=3;$i<$this->uri->total_segments();$i++)
						{
								if($this->uri->segment($i)=='lang' && $this->uri->segment($i+1) && in_array($this->uri->segment($i+1),$langs))
								{
										$lang = $this->uri->segment($i+1);
										break;
								}
						}
						if(!$lang && $this->session->userdata('sess_lang'))
						{
								$lang = $this->session->userdata('sess_lang');
						}
						if( !in_array($lang,$langs) ) $lang = $langs[0];
						if($lang) $this->session->set_userdata('sess_lang', $lang);
						return $lang;
				}
				
				function home(){
						$this->_auth_required();
						
						$data = array();
						$data['base_url'] 		= base_url();
						$opts 					= read_options('company_name', 'OBJECT');
						$data['company_name'] 	= $opts->company_name;
				 		$data['user'] = new User();
						$data['user']->where('id',(int)$this->session->userdata('uid'))->get(1);

						$this->load->library('template');
						$this->template->set_template('account');
						$this->template->write('title', $data['company_name'] . ' &middot; ' . 'Trader Room');
						$this->template->write('base_url', base_url());
						$this->template->write('csrf', $this->security->get_csrf_hash());
						$this->template->write_view('header', 'layouts/account/header', array('base_url'=>base_url()));
						$this->template->write_view('sidebar', 'layouts/account/sidebar', array('base_url'=>base_url(), 'active_menus'=>array()));
						$this->template->write_view('content', 'account/home', $data);
						$this->template->write_view('footer', 'layouts/account/footer');
						$this->template->render();
				}
				
				function accts_list(){
						$this->_auth_required();
						
						$data = array();
						$data['base_url'] 		= base_url();
						$opts 					= read_options('company_name', 'OBJECT');
						$data['company_name'] 	= $opts->company_name;
				 		$data['user'] = new User();
						$data['user']->where('id',(int)$this->session->userdata('uid'))->get(1);
						
						$this->load->library('template');
						$this->template->set_template('account');
						$this->template->write('title', $data['company_name'] . ' &middot; ' . 'Trader Room &gt; My accounts list');
						$this->template->write('base_url', base_url());
						$this->template->write('csrf', $this->security->get_csrf_hash());
						$this->template->write_view('header', 'layouts/account/header', array('base_url'=>base_url()));
						$this->template->write_view('sidebar', 'layouts/account/sidebar', array('base_url'=>base_url(), 'active_menus'=>array('my_accounts')));
						$this->template->write_view('content', 'account/accts_list', $data);
						$this->template->write_view('footer', 'layouts/account/footer');
						$this->template->render();
				}
				
				function payments_notice(){
						$this->_auth_required();
						
						$apt = array('credit_card','liberty_reserve','wire_transfer','moneybookers');
						$payment_type = $this->uri->segment(3);
						
						if( !in_array($payment_type,$apt) )
						{
								redirect(base_url() . 'account/home/');
						}
						
						if($payment_type=='wire_transfer' && $this->input->post('amount'))
						{
								$mt4_uid = '';
								$uid = $this->session->userdata('uid');
								$user = new User();
								$user->where('id', (int)$uid)->get(1);
								if($user->exists() && $user->mt4_uid!='') $mt4_uid = $user->mt4_uid;
								if($mt4_uid)
								{
										$config['upload_path'] 		= "./data/payments/$mt4_uid/";
										$config['allowed_types'] 	= 'pdf|doc|docx';
										$config['overwrite'] 		= true;
										$this->load->library('upload', $config);
										if(!is_dir($config['upload_path'])) @mkdir($config['upload_path'], 0755, true);
										
										if ( ! $this->upload->do_upload('tt_doc'))
										{
											$this->session->set_flashdata('flash-payments_notice', '<div style="color:#ff0000;weight:bold;">'.$this->upload->display_errors().'</div>');
										}
										else
										{
											$info 			= $this->upload->data();
											$full_path 		= $info['full_path'];
											$full_path_new 	= $info['file_path'] . $mt4_uid . date("YmdHi") . $info['file_ext'];
											if(@copy($full_path, $full_path_new))
											{
													@unlink($full_path);
											}
											$this->session->set_flashdata('flash-payments_notice', '<div style="color:#00ff00;weight:bold;">File successfully uploaded</div>');
										}
								}
								redirect(base_url() . 'account/payments_notice/wire_transfer');
								die();
						}
						
						$langs = array('en','fr');
						$lang = $this->_resolve_lang();
						$this->lang->load('payments_notice', $lang);
						if(!$this->lang->line('error_field_required')) $this->lang->load('payments_notice', $langs[0]);
						
						$data = array();
						$data['token'] = '';
						$data['locale']	= array (
								'lang' => $lang,
								'errors' => array(
										'error_field_required' => $this->lang->line('error_field_required')
								),
								'notice_sent' => $this->lang->line('notice_sent'),
								'msg_process' => $this->lang->line('msg_process')
						);
						$data['base_url'] = base_url();
						$opts 					= read_options('company_name', 'OBJECT');
						$data['company_name'] 	= $opts->company_name;
				 		$data['user'] = new User();
						$data['user']->where('id',(int)$this->session->userdata('uid'))->get(1);
						
						$this->load->library('template');
						$this->template->set_template('account');
						$this->template->write('title', $data['company_name'] . ' &middot; ' . 'Trader Room &gt; Bank Wire payment notification');
						$this->template->write('base_url', base_url());
						$this->template->write('csrf', $this->security->get_csrf_hash());
						$this->template->write_view('header', 'layouts/account/header', array('base_url'=>base_url()));
						$this->template->write_view('sidebar', 'layouts/account/sidebar', array('base_url'=>base_url(), 'active_menus'=>array('deposit')));
						$this->template->write_view('content', 'account/pn_'.$payment_type, $data);
						$this->template->write_view('head_content', 'sessions/script/header/login', $data);
						$this->template->write_view('footer', 'layouts/account/footer');
						$this->template->write_view('footer', 'account/script/footer/pn_'.$payment_type, array('base_url'=>base_url()));
						$this->template->render();
				}
				
				function ajax_payments_notice(){
						if( $this->input->is_ajax_request() && $this->input->post('type') )
						{
								$this->_auth_required('ajax', base_url().'account/home/');
								
								$apt = array('credit_card','liberty_reserve','wire_transfer','moneybookers');
								
								if( in_array($this->input->post('type'),$apt) && method_exists($this, "_process_".$this->input->post('type')."_notice") )
								{
										return call_user_func_array(array($this, "_process_".$this->input->post('type')."_notice"), array());
								}
								else
								{
										die( json_encode(array('stat'=>'error', 'field_id'=>'amount', 'msg'=>'Request handler not defined')) );
								}
								
						}
						else
						{
								die( json_encode(array('stat'=>'error', 'field_id'=>'amount', 'msg'=>'Malformed request')) );
						}
				}
				
				function _process_credit_card_notice(){
						die( json_encode(array('stat'=>'success', 'msg'=>'Credit card notice successfully processed!')) );
				}
				
				function _process_liberty_reserve_notice(){
						$opts 		= read_options('', 'OBJECT');
						
						$client = new User();
						$client->where('id',(int)$this->session->userdata('uid'))->get(1);
						
						$tpl_email  = new Tpl_email(); $tpl_email->where('type','liberty_reserve_payment_notice')->get(1);
						$date 		= ($this->input->post('pday').'/'.$this->input->post('pmonth').'/'.$this->input->post('pyear'));
						$subject	= $this->_map_lr_presets($tpl_email->subject, $client->id, $this->input->post('amount'), $this->input->post('acct'), $this->input->post('batch_code'), $date);
						$subject 	= $this->_map_option_presets($subject);
						$message 	= $this->_map_lr_presets($tpl_email->message, $client->id, $this->input->post('amount'), $this->input->post('acct'), $this->input->post('batch_code'), $date);
						$message 	= $this->_map_option_presets($message);
						
						$this->load->library('email');
						$config['protocol'] 	= 'smtp';
						$config['smtp_host'] 	= ($opts->smtp_ssl=='yes'&& substr(strtolower($opts->smtp_host), 0, 6)!='ssl://' ? 'ssl://' : '') . $opts->smtp_host;
						$config['smtp_user'] 	= $opts->smtp_user;
						$config['smtp_pass'] 	= $opts->smtp_pass;
						$config['smtp_port'] 	= $opts->smtp_port;
						$config['mailtype'] 	= 'html';
						$config['charset'] 		= 'utf-8';
						$config['wordwrap']  	= TRUE;
						$this->email->initialize($config);
						$this->email->set_newline("\r\n");
						
						$this->email->from($opts->smtp_user, $opts->company_name);
						$this->email->to($opts->admin_email);
						$this->email->subject($subject);
						$this->email->message($message);
						$this->email->send();
						
						log_activity('Liberty Reserve payment notice sent by ' . $this->_get_current_user_name() . ' (MT4 #' . $this->_get_current_user_mt4_uid() . ')', 'user', 'payment_notice', 'system');
						
						die( json_encode(array('stat'=>'success', 'msg'=>'Liberty reserve notice successfully processed!')) );
				}
				
				function _process_wire_transfer_notice(){
						$opts 		= read_options('', 'OBJECT');
						
				 		$client = new User();
						$client->where('id',(int)$this->session->userdata('uid'))->get(1);
						
						$tpl_email  = new Tpl_email(); $tpl_email->where('type','wire_payment_notice')->get(1);
						$date 		= ($this->input->post('pday').'/'.$this->input->post('pmonth').'/'.$this->input->post('pyear'));
						$subject	= $this->_map_wire_presets($tpl_email->subject, $client->id, $this->input->post('currency'), $this->input->post('amount'), $date, $this->input->post('bank_name'), $this->input->post('ac_name'), $this->input->post('ac_no'), $this->input->post('benef_bank'), $this->input->post('benef_ac_no'), $this->input->post('payment_id'));
						$subject 	= $this->_map_option_presets($subject);
						$message 	= $this->_map_wire_presets($tpl_email->message, $client->id, $this->input->post('currency'), $this->input->post('amount'), $date, $this->input->post('bank_name'), $this->input->post('ac_name'), $this->input->post('ac_no'), $this->input->post('benef_bank'), $this->input->post('benef_ac_no'), $this->input->post('payment_id'));
						$message 	= $this->_map_option_presets($message);
						
						$this->load->library('email');
						$config['protocol'] 	= 'smtp';
						$config['smtp_host'] 	= ($opts->smtp_ssl=='yes'&& substr(strtolower($opts->smtp_host), 0, 6)!='ssl://' ? 'ssl://' : '') . $opts->smtp_host;
						$config['smtp_user'] 	= $opts->smtp_user;
						$config['smtp_pass'] 	= $opts->smtp_pass;
						$config['smtp_port'] 	= $opts->smtp_port;
						$config['mailtype'] 	= 'html';
						$config['charset'] 		= 'utf-8';
						$config['wordwrap']  	= TRUE;
						$this->email->initialize($config);
						$this->email->set_newline("\r\n");
						
						$this->email->from($opts->smtp_user, $opts->company_name);
						// if($this->input->ip_address()=='41.206.77.96') $this->email->to('holy.works01@gmail.com');
						// else $this->email->to($opts->admin_email);
						$this->email->to($opts->admin_email);
						$this->email->subject($subject);
						$this->email->message($message);
						$this->email->send();
						
						log_activity('Wire Transfer payment notice sent by ' . $this->_get_current_user_name() . ' (MT4 #' . $this->_get_current_user_mt4_uid() . ')', 'user', 'payment_notice', 'system');
						
						die( json_encode(array('stat'=>'success', 'msg'=>'Wire transfer notice successfully processed!')) );
				}
				
				function _process_moneybookers_notice(){
						$opts 		= read_options('', 'OBJECT');
						
						$client = new User();
						$client->where('id',(int)$this->session->userdata('uid'))->get(1);
						
						$tpl_email  = new Tpl_email(); $tpl_email->where('type','moneybookers_payment_notice')->get(1);
						$date 		= ($this->input->post('pday').'/'.$this->input->post('pmonth').'/'.$this->input->post('pyear'));
						$subject	= $this->_map_mb_presets($tpl_email->subject, $client->id, $this->input->post('amount'), $date, $this->input->post('purse'));
						$subject 	= $this->_map_option_presets($subject);
						$message 	= $this->_map_mb_presets($tpl_email->message, $client->id, $this->input->post('amount'), $date, $this->input->post('purse'));
						$message 	= $this->_map_option_presets($message);
						
						$this->load->library('email');
						$config['protocol'] 	= 'smtp';
						$config['smtp_host'] 	= ($opts->smtp_ssl=='yes'&& substr(strtolower($opts->smtp_host), 0, 6)!='ssl://' ? 'ssl://' : '') . $opts->smtp_host;
						$config['smtp_user'] 	= $opts->smtp_user;
						$config['smtp_pass'] 	= $opts->smtp_pass;
						$config['smtp_port'] 	= $opts->smtp_port;
						$config['mailtype'] 	= 'html';
						$config['charset'] 		= 'utf-8';
						$config['wordwrap']  	= TRUE;
						$this->email->initialize($config);
						$this->email->set_newline("\r\n");
						
						$this->email->from($opts->smtp_user, $opts->company_name);
						$this->email->to($opts->admin_email);
						$this->email->subject($subject);
						$this->email->message($message);
						$this->email->send();
						
						log_activity('Moneybookers payment notice sent by ' . $this->_get_current_user_name() . ' (MT4 #' . $this->_get_current_user_mt4_uid() . ')', 'user', 'payment_notice', 'system');
						
						die( json_encode(array('stat'=>'success', 'msg'=>'Moneybookers notice successfully processed!')) );
				}
				
				function _map_client_presets($user_id, $str) {
						$user = new User();
						$user->where('id',(int)$user_id)->get(1);
						$find = array(
								'{client_id}', 
								'{client_name}', 
								'{client_first_name}', 
								'{client_last_name}', 
								'{client_country}', 
								'{client_email}', 
								'{client_password}', 
								'{client_phonenumber}', 
								'{client_address1}', 
								'{client_address2}', 
								'{client_city}', 
								'{client_state}', 
								'{client_company}', 
								'{client_postcode}', 
								'{client_mt4_id}', 
								'{client_mirror_login}', 
								'{client_group}', 
								'{client_mt4_agent}', 
								'{client_leverage}', 
								'{client_stored_doc}', 
								'{client_signature_image}'
						);
						$replaceby = array(
								$user->id,
								$user->firstname . ($user->firstname&&$user->lastname?' ':'') . $user->lastname,
								$user->firstname,
								$user->lastname,
								$user->country,
								$user->email,
								$this->encrypt->decode($user->password),
								$user->phone,
								$user->address1,
								$user->address2,
								$user->city,
								$user->state,
								$user->business,
								$user->zip,
								$user->mt4_uid,
								$user->mirror_login_id,
								$user->mt4_group,
								$user->mt4_agent,
								$user->leverage,
								$user->stored_doc ? base_url().'data/'.$user->id.'/'.$user->stored_doc : '',
								$user->signature_image_200x122 ? '<img src="'.base_url().'data/'.$user->id.'/'.$user->signature_image_200x122.'" />' : ''
						);
						return str_replace($find,$replaceby,$str);
				}
				
				function _map_option_presets($str) {
						$opts = read_options('', 'OBJECT');
						$find = array(
							'{admin_email}',
							'{company_name}',
							'{logo}',
						);
						$replaceby = array(
							$opts->admin_email,
							$opts->company_name,
							'<img src="'.$opts->logo.'"'.($opts->logo_width?' width="'.$opts->logo_width.'"':'').($opts->logo_height?' height="'.$opts->logo_height.'"':'').'>',
						);
						return str_replace($find,$replaceby,$str);
				}
				
				function _map_lr_presets($str, $user_id, $amount, $account, $batch_code, $date) {
						$user = new User();
						$user->where('id',(int)$user_id)->get(1);
						$find = array(
								'{client_id}', 
								'{client_name}', 
								'{client_first_name}', 
								'{client_last_name}', 
								'{client_country}', 
								'{client_email}', 
								'{client_password}', 
								'{client_phonenumber}', 
								'{client_address1}', 
								'{client_address2}', 
								'{client_city}', 
								'{client_state}', 
								'{client_company}', 
								'{client_postcode}', 
								'{client_mt4_id}', 
								'{client_mirror_login}', 
								'{client_group}', 
								'{client_mt4_agent}', 
								'{client_leverage}', 
								'{client_stored_doc}', 
								'{client_signature_image}',
								'{payment_amount}',
								'{liberty_reserve_account}',
								'{liberty_reserve_batch_code}',
								'{payment_date}'
						);
						$replaceby = array(
								$user->id,
								$user->firstname . ($user->firstname&&$user->lastname?' ':'') . $user->lastname,
								$user->firstname,
								$user->lastname,
								$user->country,
								$user->email,
								$this->encrypt->decode($user->password),
								$user->phone,
								$user->address1,
								$user->address2,
								$user->city,
								$user->state,
								$user->business,
								$user->zip,
								$user->mt4_uid,
								$user->mirror_login_id,
								$user->mt4_group,
								$user->mt4_agent,
								$user->leverage,
								$user->stored_doc ? base_url().'data/'.$user->id.'/'.$user->stored_doc : '',
								$user->signature_image_200x122 ? '<img src="'.base_url().'data/'.$user->id.'/'.$user->signature_image_200x122.'" />' : '',
								$amount,
								$account,
								$batch_code,
								$date
						);
						return str_replace($find,$replaceby,$str);
				}
				
				function _map_wire_presets($str, $user_id, $currency, $amount, $date, $sender_bank, $sender_ac_name, $sender_bank_ac_no, $beneficiary_bank, $beneficiary_ac_no, $payment_id) {
						$user = new User();
						$user->where('id',(int)$user_id)->get(1);
						$find = array(
								'{client_id}', 
								'{client_name}', 
								'{client_first_name}', 
								'{client_last_name}', 
								'{client_country}', 
								'{client_email}', 
								'{client_password}', 
								'{client_phonenumber}', 
								'{client_address1}', 
								'{client_address2}', 
								'{client_city}', 
								'{client_state}', 
								'{client_company}', 
								'{client_postcode}', 
								'{client_mt4_id}', 
								'{client_mirror_login}', 
								'{client_group}', 
								'{client_mt4_agent}', 
								'{client_leverage}', 
								'{client_stored_doc}', 
								'{client_signature_image}',
								'{currency}',
								'{payment_amount}',
								'{payment_date}',
								'{sender_bank}',
								'{sender_ac_name}',
								'{sender_bank_ac_no}',
								'{beneficiary_bank}',
								'{beneficiary_ac_no}',
								'{payment_id}'
						);
						$replaceby = array(
								$user->id,
								$user->firstname . ($user->firstname&&$user->lastname?' ':'') . $user->lastname,
								$user->firstname,
								$user->lastname,
								$user->country,
								$user->email,
								$this->encrypt->decode($user->password),
								$user->phone,
								$user->address1,
								$user->address2,
								$user->city,
								$user->state,
								$user->business,
								$user->zip,
								$user->mt4_uid,
								$user->mirror_login_id,
								$user->mt4_group,
								$user->mt4_agent,
								$user->leverage,
								$user->stored_doc ? base_url().'data/'.$user->id.'/'.$user->stored_doc : '',
								$user->signature_image_200x122 ? '<img src="'.base_url().'data/'.$user->id.'/'.$user->signature_image_200x122.'" />' : '',
								$currency,
								$amount, 
								$date, 
								$sender_bank, 
								$sender_ac_name, 
								$sender_bank_ac_no, 
								$beneficiary_bank, 
								$beneficiary_ac_no, 
								$payment_id
						);
						return str_replace($find,$replaceby,$str);
				}
				
				function _map_mb_presets($str, $user_id, $amount, $date, $purse) {
						$user = new User();
						$user->where('id',(int)$user_id)->get(1);
						$find = array(
								'{client_id}', 
								'{client_name}', 
								'{client_first_name}', 
								'{client_last_name}', 
								'{client_country}', 
								'{client_email}', 
								'{client_password}', 
								'{client_phonenumber}', 
								'{client_address1}', 
								'{client_address2}', 
								'{client_city}', 
								'{client_state}', 
								'{client_company}', 
								'{client_postcode}', 
								'{client_mt4_id}', 
								'{client_mirror_login}', 
								'{client_group}', 
								'{client_mt4_agent}', 
								'{client_leverage}', 
								'{client_stored_doc}', 
								'{client_signature_image}',
								'{payment_amount}',
								'{payment_date}',
								'{purse}'								
						);
						$replaceby = array(
								$user->id,
								$user->firstname . ($user->firstname&&$user->lastname?' ':'') . $user->lastname,
								$user->firstname,
								$user->lastname,
								$user->country,
								$user->email,
								$this->encrypt->decode($user->password),
								$user->phone,
								$user->address1,
								$user->address2,
								$user->city,
								$user->state,
								$user->business,
								$user->zip,
								$user->mt4_uid,
								$user->mirror_login_id,
								$user->mt4_group,
								$user->mt4_agent,
								$user->leverage,
								$user->stored_doc ? base_url().'data/'.$user->id.'/'.$user->stored_doc : '',
								$user->signature_image_200x122 ? '<img src="'.base_url().'data/'.$user->id.'/'.$user->signature_image_200x122.'" />' : '',
								$amount,
								$date,
								$purse
						);
						return str_replace($find,$replaceby,$str);
				}
				
				function deposit_cc(){
						$this->_auth_required();
						
						$opts = read_options('company_name', 'OBJECT');
						
						$data = array();
						$data['base_url'] 		= base_url();
						$data['company_name'] 	= $opts->company_name;
				 		$data['user'] = new User();
						$data['user']->where('id',(int)$this->session->userdata('uid'))->get(1);

						$this->load->library('template');
						$this->template->set_template('account');
						$this->template->write('title', $data['company_name'] . ' &middot; ' . 'Trader Room &gt; Account deposit by Credit Card');
						$this->template->write('base_url', base_url());
						$this->template->write('csrf', $this->security->get_csrf_hash());
						$this->template->write_view('header', 'layouts/account/header', array('base_url'=>base_url()));
						$this->template->write_view('sidebar', 'layouts/account/sidebar', array('base_url'=>base_url(), 'active_menus'=>array('deposit')));
						$this->template->write_view('content', 'account/deposit_cc', $data);
						$this->template->write_view('footer', 'layouts/account/footer');
						$this->template->render();
				}
				
				function deposit_lr(){
						$this->_auth_required();
						
						$opts = read_options('company_name', 'OBJECT');
						
						$data = array();
						$data['base_url'] 		= base_url();
						$data['company_name'] 	= $opts->company_name;
				 		$data['user'] = new User();
						$data['user']->where('id',(int)$this->session->userdata('uid'))->get(1);

						$this->load->library('template');
						$this->template->set_template('account');
						$this->template->write('title', $data['company_name'] . ' &middot; ' . 'Trader Room &gt; Account deposit by Liberty Reserve');
						$this->template->write('base_url', base_url());
						$this->template->write('csrf', $this->security->get_csrf_hash());
						$this->template->write_view('header', 'layouts/account/header', array('base_url'=>base_url()));
						$this->template->write_view('sidebar', 'layouts/account/sidebar', array('base_url'=>base_url(), 'active_menus'=>array('deposit')));
						$this->template->write_view('content', 'account/deposit_lr', $data);
						$this->template->write_view('footer', 'layouts/account/footer');
						$this->template->render();
				}
				
				function deposit_wire(){
						$this->_auth_required();
						
						$opts = read_options('company_name', 'OBJECT');
						
						$data = array();
						$data['base_url'] 		= base_url();
						$data['company_name'] 	= $opts->company_name;
				 		$data['user'] = new User();
						$data['user']->where('id',(int)$this->session->userdata('uid'))->get(1);

						$this->load->library('template');
						$this->template->set_template('account');
						$this->template->write('title', $data['company_name'] . ' &middot; ' . 'Trader Room &gt; Account deposit by Wire Transfer');
						$this->template->write('base_url', base_url());
						$this->template->write('csrf', $this->security->get_csrf_hash());
						$this->template->write_view('header', 'layouts/account/header', array('base_url'=>base_url()));
						$this->template->write_view('sidebar', 'layouts/account/sidebar', array('base_url'=>base_url(), 'active_menus'=>array('deposit')));
						$this->template->write_view('content', 'account/deposit_wire', $data);
						$this->template->write_view('footer', 'layouts/account/footer');
						$this->template->render();
				}
				
				function deposit_mb(){
						$this->_auth_required();
						
						$opts = read_options('company_name', 'OBJECT');
						
						$data = array();
						$data['base_url'] 		= base_url();
						$data['company_name'] 	= $opts->company_name;
				 		$data['user'] = new User();
						$data['user']->where('id',(int)$this->session->userdata('uid'))->get(1);

						$this->load->library('template');
						$this->template->set_template('account');
						$this->template->write('title', $data['company_name'] . ' &middot; ' . 'Trader Room &gt; Account deposit by MoneyBookers');
						$this->template->write('base_url', base_url());
						$this->template->write('csrf', $this->security->get_csrf_hash());
						$this->template->write_view('header', 'layouts/account/header', array('base_url'=>base_url()));
						$this->template->write_view('sidebar', 'layouts/account/sidebar', array('base_url'=>base_url(), 'active_menus'=>array('deposit')));
						$this->template->write_view('content', 'account/deposit_mb', $data);
						$this->template->write_view('footer', 'layouts/account/footer');
						$this->template->render();
				}
				
				function withdrawal(){
						$this->_auth_required();
						
						$opts = read_options('company_name', 'OBJECT');
						
						$data = array();
						$data['base_url'] 		= base_url();
						$data['company_name'] 	= $opts->company_name;
				 		$data['user'] = new User();
						$data['user']->where('id',(int)$this->session->userdata('uid'))->get(1);

						$this->load->library('template');
						$this->template->set_template('account');
						$this->template->write('title', $data['company_name'] . ' &middot; ' . 'Trader Room &gt; Withdrawal request');
						$this->template->write('base_url', base_url());
						$this->template->write('csrf', $this->security->get_csrf_hash());
						$this->template->write_view('header', 'layouts/account/header', array('base_url'=>base_url()));
						$this->template->write_view('sidebar', 'layouts/account/sidebar', array('base_url'=>base_url(), 'active_menus'=>array('withdrawal')));
						$this->template->write_view('content', 'account/withdrawal', $data);
						$this->template->write_view('footer', 'layouts/account/footer');
						$this->template->render();
				}
				
				function affclients(){
						$this->_auth_required();
						
						$opts = read_options('company_name', 'OBJECT');
						
						$data = array();
						$data['base_url'] 		= base_url();
						$data['company_name'] 	= $opts->company_name;
				 		$data['user'] = new User();
						$data['user']->where('id',(int)$this->session->userdata('uid'))->get(1);
						$data['refs'] = new User();
						$data['refs']->where('mt4_agent',(int)$data['user']->mt4_uid)->get();
						
						$this->load->library('template');
						$this->template->set_template('account');
						$this->template->write('title', $data['company_name'] . ' &middot; ' . 'Trader Room &gt; My Clients');
						$this->template->write('base_url', base_url());
						$this->template->write('csrf', $this->security->get_csrf_hash());
						$this->template->write_view('header', 'layouts/account/header', array('base_url'=>base_url()));
						$this->template->write_view('sidebar', 'layouts/account/sidebar', array('base_url'=>base_url(), 'active_menus'=>array('affiliate')));
						$this->template->write_view('content', 'account/affclients', $data);
						$this->template->write_view('footer', 'layouts/account/footer');
						$this->template->render();
				}
				
				function subib_recruit_link(){
						$this->_auth_required();
						
						$opts = read_options('company_name', 'OBJECT');
						
						$data = array();
						$data['base_url'] 		= base_url();
						$data['company_name'] 	= $opts->company_name;
				 		$data['user'] = new User();
						$data['user']->where('id',(int)$this->session->userdata('uid'))->get(1);
						$data['refs'] = new User();
						$data['refs']->where('mt4_agent',(int)$data['user']->mt4_uid)->get();
						
						$this->load->library('template');
						$this->template->set_template('account');
						$this->template->write('title', $data['company_name'] . ' &middot; ' . 'Trader Room &gt; Sub-IB Recruitment Link');
						$this->template->write('base_url', base_url());
						$this->template->write('csrf', $this->security->get_csrf_hash());
						$this->template->write_view('header', 'layouts/account/header', array('base_url'=>base_url()));
						$this->template->write_view('sidebar', 'layouts/account/sidebar', array('base_url'=>base_url(), 'active_menus'=>array('affiliate')));
						$this->template->write_view('content', 'account/recruit_link', $data);
						$this->template->write_view('footer', 'layouts/account/footer');
						$this->template->render();
				}
				
				function logs(){
						$this->_auth_required();
						
						$opts = read_options('company_name', 'OBJECT');
						
						$data = array();
						$data['base_url'] 		= base_url();
						$data['company_name'] 	= $opts->company_name;
				 		$data['user'] = new User();
						$data['user']->where('id',(int)$this->session->userdata('uid'))->get(1);

						$this->load->view('account_logs', $data);
				}
				
				function afflink(){
						$this->_auth_required();
						
						$opts = read_options('company_name', 'OBJECT');
						
						$data = array();
						$data['afflink'] 		= '';
						$data['base_url'] 		= base_url();
						$data['company_name'] 	= $opts->company_name;
				 		$data['user'] = new User();
						$data['user']->where('id',(int)$this->session->userdata('uid'))->get(1);
						
						if($this->input->post('type') && $this->input->post('lang'))
						{
								if($this->input->post('type')==-1)
								{
										$Afflink = new Afflink();
										$token 	 = random_string('unique');
										$Afflink->where('token',$token)->get(1);
										while($Afflink->exists())
										{
												$Afflink->clear();
												$token 	 = random_string('unique');
												$Afflink->where('token',$token)->get(1);
										}
										$Afflink->token 	= $token;
										$Afflink->user_id 	= $this->session->userdata('uid');
										$Afflink->dest 		= trim($this->input->post('custom_url'));
										$Afflink->added 	= time();
										$Afflink->save();
										$data['afflink'] 	= base_url() . 'ref/' . $data['user']->id . '/token/' . $token . '/op/cust/';
								}
								else
								{
										$link = new Ref_link();
										$link->where('id', (int)$this->input->post('type'))->get(1);
										if($link->exists())
										{
												$data['afflink'] = base_url() . 'ref/' . $data['user']->id . '/op/' . $link->id . '/';
										}
								}
								if($data['afflink'] && $this->input->post('lang') && in_array($this->input->post('lang'),array('en','fr')))
								$data['afflink'] .= 'lang/'.$this->input->post('lang');
								
								// if($this->input->post('type')==1)
								// {
										// $data['afflink'] = base_url() . 'ref/' . $data['user']->id . '/op/hm/';
								// }
								// elseif($this->input->post('type')==2)
								// {
										// $data['afflink'] = base_url() . 'ref/' . $data['user']->id . '/op/sc/';
								// }
								// elseif($this->input->post('type')==3)
								// {
										// $data['afflink'] = base_url() . 'ref/' . $data['user']->id . '/op/mt4/';
								// }
								// elseif($this->input->post('type')==4)
								// {
										// $data['afflink'] = base_url() . 'ref/' . $data['user']->id . '/op/mrt/';
								// }
								// elseif($this->input->post('type')==5)
								// {
										// $data['afflink'] = base_url() . 'ref/' . $data['user']->id . '/op/fxtb/';
								// }
								// elseif($this->input->post('type')==6)
								// {
										// $data['afflink'] = base_url() . 'ref/' . $data['user']->id . '/op/fxed/';
								// }
								// elseif($this->input->post('type')==7)
								// {
										// $data['afflink'] = base_url() . 'ref/' . $data['user']->id . '/op/lv/';
								// }
								// elseif($this->input->post('type')==8)
								// {
										// $data['afflink'] = base_url() . 'ref/' . $data['user']->id . '/op/dm/';
								// }
								// elseif($this->input->post('type')==9)
								// {
										// $Afflink = new Afflink();
										// $token 	 = random_string('unique');
										// $Afflink->where('token',$token)->get(1);
										// while($Afflink->exists())
										// {
												// $Afflink->clear();
												// $token 	 = random_string('unique');
												// $Afflink->where('token',$token)->get(1);
										// }
										// $Afflink->token 	= $token;
										// $Afflink->user_id 	= $this->session->userdata('uid');
										// $Afflink->dest 		= trim($this->input->post('custom_url'));
										// $Afflink->added 	= time();
										// $Afflink->save();
										// $data['afflink'] 	= base_url() . 'ref/' . $data['user']->id . '/token/' . $token . '/op/cust/';
								// }
								// elseif($this->input->post('type')==10)
								// {
										// $Afflink = new Afflink();
										// $token 	 = random_string('unique');
										// $Afflink->where('token',$token)->get(1);
										// while($Afflink->exists())
										// {
												// $Afflink->clear();
												// $token 	 = random_string('unique');
												// $Afflink->where('token',$token)->get(1);
										// }
										// $Afflink->token 	= $token;
										// $Afflink->user_id 	= $this->session->userdata('uid');
										// $Afflink->dest 		= 'http://platform.tradency.com/practiceform/default.aspx?Language=en-US&Token=4d92890d79cc483d9016aa2e2cbf893a&changeLang=0';
										// $Afflink->added 	= time();
										// $Afflink->save();
										// $data['afflink'] 	= base_url() . 'ref/' . $data['user']->id . '/token/' . $token . '/op/cust/';
								// }
								// elseif($this->input->post('type')==11)
								// {
										// $Afflink = new Afflink();
										// $token 	 = random_string('unique');
										// $Afflink->where('token',$token)->get(1);
										// while($Afflink->exists())
										// {
												// $Afflink->clear();
												// $token 	 = random_string('unique');
												// $Afflink->where('token',$token)->get(1);
										// }
										// $Afflink->token 	= $token;
										// $Afflink->user_id 	= $this->session->userdata('uid');
										// $Afflink->dest 		= 'http://platform.tradency.com/b71/GolderMarkets/';
										// $Afflink->added 	= time();
										// $Afflink->save();
										// $data['afflink'] 	= base_url() . 'ref/' . $data['user']->id . '/token/' . $token . '/op/cust/';
								// }								
						}						
						
						$this->load->library('template');
						$this->template->set_template('account');
						$this->template->write('title', $data['company_name'] . ' &middot; ' . 'Trader Room &gt; My referral link');
						$this->template->write('base_url', base_url());
						$this->template->write('csrf', $this->security->get_csrf_hash());
						$this->template->write_view('header', 'layouts/account/header', array('base_url'=>base_url()));
						$this->template->write_view('sidebar', 'layouts/account/sidebar', array('base_url'=>base_url(), 'active_menus'=>array('affiliate')));
						$this->template->write_view('content', 'account/afflink', $data);
						$this->template->write_view('footer', 'layouts/account/footer');
						$this->template->render();
				}
				
				function password_center(){
						$this->_auth_required();
						// $this->load->helper('captcha');
						
						// $data = array();
						// $vals = array(
							// 'img_path' => './images/captcha/',
							// 'img_url' => base_url() . 'images/captcha/',
						// );
						// $data['captcha'] = create_captcha($vals);
						// $captcha = new Captcha();
						// $captcha->captcha_time 	= $data['captcha']['time'];
						// $captcha->ip_address 	= $this->input->ip_address();
						// $captcha->word 			= $data['captcha']['word'];
						// $captcha->save();
						
						// $this->_purge_captchas();
						
						$opts = read_options('company_name', 'OBJECT');
						
						$data['base_url'] 		= base_url();
						$data['company_name'] 	= $opts->company_name;
				 		$data['user'] = new User();
						$data['user']->where('id',(int)$this->session->userdata('uid'))->get(1);

						$this->load->library('template');
						$this->template->set_template('account');
						$this->template->write('title', $data['company_name'] . ' &middot; ' . 'Trader Room &gt; Password Centre');
						$this->template->write('base_url', base_url());
						$this->template->write('csrf', $this->security->get_csrf_hash());
						$this->template->write_view('header', 'layouts/account/header', array('base_url'=>base_url()));
						$this->template->write_view('sidebar', 'layouts/account/sidebar', array('base_url'=>base_url(), 'active_menus'=>array('security')));
						$this->template->write_view('content', 'account/password_center', $data);
						$this->template->write_view('footer', 'layouts/account/footer');
						$this->template->write_view('footer', 'account/script/footer/password_center', array('base_url'=>base_url()));
						$this->template->render();
				}				
				
				function manage_banners() {
						$this->_auth_required();
						
						$opts = read_options('company_name', 'OBJECT');
						
						$data = array();
						$data['base_url'] 		= base_url();
						$data['company_name'] 	= $opts->company_name;
						
						$data['banners'] = new Banner();
						if($this->session->userdata('logged_in') && $this->session->userdata('uid')>0)
						{
								$data['banners']->where('user_id', (int)$this->session->userdata('uid'))
												->order_by('id', 'desc')
											    ->get();
						}
												
						$this->load->library('template');
						$this->template->set_template('account');
						$this->template->write('title', $data['company_name'] . ' &middot; ' . 'Trader Room &gt; Manage Banners');
						$this->template->write('base_url', base_url());
						$this->template->write('csrf', $this->security->get_csrf_hash());
						$this->template->write_view('header', 'layouts/account/header', array('base_url'=>base_url()));
						$this->template->write_view('sidebar', 'layouts/account/sidebar', array('base_url'=>base_url(), 'active_menus'=>array('affiliate')));
						$this->template->write_view('content', 'account/manage_banners', $data);
						$this->template->write_view('footer', 'layouts/account/footer');
						$this->template->render();
				}
						
				function add_banner() {
						$this->_auth_required();
						
						$opts = read_options('company_name', 'OBJECT');
						
						$data = array();
						$data['base_url'] 		= base_url();
						$data['company_name'] 	= $opts->company_name;
												
						$this->load->library('template');
						$this->template->set_template('account');
						$this->template->write('title', $data['company_name'] . ' &middot; ' . 'Trader Room &gt; Add Banner');
						$this->template->write('base_url', base_url());
						$this->template->write('csrf', $this->security->get_csrf_hash());
						$this->template->write_view('header', 'layouts/account/header', array('base_url'=>base_url()));
						$this->template->write_view('sidebar', 'layouts/account/sidebar', array('base_url'=>base_url(), 'active_menus'=>array('affiliate')));
						$this->template->write_view('content', 'account/add_banner', $data);
						$this->template->write_view('footer', 'layouts/account/footer');
						$this->template->render();
				}
						
				function edit_banner() {
						$this->_auth_required();
						
						$uri = $this->uri->uri_to_assoc(3);
						if(!isset($uri['id'])) redirect(base_url() . 'account/manage_banners');
						
						$data 			   = array();
						$banner_id 		   = (int)$uri['id'];
						$data['banner']    = new Banner();
						$data['banner']->where('user_id', (int)$this->session->userdata('uid'))
									   ->where('user_id >', 0)
									   ->where('id', $banner_id)
									   ->where('id >', 0)
									   ->get(1);
						if(!$data['banner']->exists()) redirect(base_url() . 'account/manage_banners');
						
						$opts = read_options('company_name', 'OBJECT');
						
						$data['base_url'] 		= base_url();
						$data['company_name'] 	= $opts->company_name;
												
						$this->load->library('template');
						$this->template->set_template('account');
						$this->template->write('title', $data['company_name'] . ' &middot; ' . 'Trader Room &gt; Edit Banner'.($data['banner']->name?' &gt; ' . $data['banner']->name:''));
						$this->template->write('base_url', base_url());
						$this->template->write('csrf', $this->security->get_csrf_hash());
						$this->template->write_view('header', 'layouts/account/header', array('base_url'=>base_url()));
						$this->template->write_view('sidebar', 'layouts/account/sidebar', array('base_url'=>base_url(), 'active_menus'=>array('affiliate')));
						$this->template->write_view('content', 'account/edit_banner', $data);
						$this->template->write_view('footer', 'layouts/account/footer');
						$this->template->render();
				}		
						
				function upload_banner() {
						$data = array();
						
						$config['upload_path'] 		= './images/banners';
						if(!is_dir($config['upload_path'])) @mkdir($config['upload_path'], 0755);
						$config['allowed_types'] 	= 'gif|jpg|jpeg|jpe|png';
						$config['max_size']			= '3000';
						$config['max_width']  		= '2000';
						$config['max_height']  		= '2000';
						$config['encrypt_name']  	= TRUE;

						$this->load->library('upload', $config);
						if ( isset($_FILES['banner']['name']))
						{
							if ( ! $this->upload->do_upload('banner'))
							{
								$data['error'] = str_replace(array('<p>','</p>'), '', $this->upload->display_errors());
							}
							else
							{
								$data['upload_data'] = $this->upload->data();
								$data['upload_data']['image_size_str'] = '';
								
								$banner_id = (int)$this->input->post('banner_id');
								$data['upload_data']['banner_id'] = $banner_id;
								$data['upload_data']['type'] 	  = $this->input->post('type');
								$banner = new Banner();
								$banner->where('id', $banner_id)
									   ->where('id >', 0)
									   ->get(1);
								if($banner->exists())
								{
									if($banner->width>0 && $banner->height>0)
									{
											$this->load->library('image_lib');
											$config['image_library'] 	= 'gd2';
											$config['source_image']  	= $data['upload_data']['full_path'];
											$config['new_image']  		= $banner->width . 'x' . $banner->height . '_' . $data['upload_data']['file_name'];
											// $config['maintain_ratio'] 	= TRUE;
											$config['create_thumb'] 	= FALSE;
											$config['thumb_marker'] 	= $banner->width . 'x' . $banner->height;
											$config['width'] 			= $banner->width;
											$config['height'] 			= $banner->height;
											$this->image_lib->initialize($config);
											$check 						= $this->image_lib->resize();
									}
									$banner->clear();
									$banner->where('id', $banner_id)->update(array('image'=> @$data['upload_data']['file_name'], 'b_updated'=>time()));
								}
								
							}
						}
						
						$this->load->view('account/upload_banner', $data);
				}		
						
				function embed_banner() {
						$this->_auth_required();
						
						$opts = read_options('company_name', 'OBJECT');
						
						$data = array();
						$data['base_url'] 		= base_url();
						$data['company_name'] 	= $opts->company_name;
						
						$data['banners'] = new Banner();
						$data['banners']->order_by('id', 'desc')->get();
												
						$this->load->library('template');
						$this->template->set_template('account');
						$this->template->write('title', $data['company_name'] . ' &middot; ' . 'Trader Room &gt; Embed Banner');
						$this->template->write('base_url', base_url());
						$this->template->write('csrf', $this->security->get_csrf_hash());
						$this->template->write_view('header', 'layouts/account/header', array('base_url'=>base_url()));
						$this->template->write_view('sidebar', 'layouts/account/sidebar', array('base_url'=>base_url(), 'active_menus'=>array('affiliate')));
						$this->template->write_view('content', 'account/embed_banner', $data);
						$this->template->write_view('footer', 'layouts/account/footer');
						$this->template->render();
				}
					
				function ajax_add_banner() {
						if( $this->input->is_ajax_request() && $this->input->post('name') && $this->input->post('dest') && $this->input->post('width') && $this->input->post('height') ) 
						{
								 $name 		= trim($this->security->xss_clean($this->input->post('name')));
								 $dest 		= trim($this->security->xss_clean($this->input->post('dest')));
								 $alt 		= trim($this->security->xss_clean($this->input->post('alt')));
								 $width 	= (int)$this->input->post('width');
								 $height 	= (int)$this->input->post('height');
								 $target 	= trim($this->security->xss_clean($this->input->post('target')));
								 
								 if($this->session->userdata('logged_in') && $this->session->userdata('uid')>0)
								 {
										$banner 			= new Banner();
										$banner->user_id 	= $this->session->userdata('uid');
										$banner->name 		= $name;
										$banner->dest 		= $dest;
										$banner->alt 		= $alt;
										$banner->width 		= $width;
										$banner->height 	= $height;
										$banner->target 	= $target;
										$banner->b_added 	= time();
										$banner->save();
										
										die(json_encode(array(
												'stat'		=>'success',
												'banner_id'	=>$this->db->insert_id()
										)));
								 }
						}
				}
				
				function ajax_save_banner() {
						if( $this->input->is_ajax_request()&& $this->input->post('banner_id') && $this->input->post('name') && $this->input->post('dest') && $this->input->post('width') && $this->input->post('height') ) 
						{
								 $banner_id = (int)$this->input->post('banner_id');
								 $name 		= trim($this->security->xss_clean($this->input->post('name')));
								 $dest 		= trim($this->security->xss_clean($this->input->post('dest')));
								 $alt 		= trim($this->security->xss_clean($this->input->post('alt')));
								 $width 	= (int)$this->input->post('width');
								 $height 	= (int)$this->input->post('height');
								 $target 	= trim($this->security->xss_clean($this->input->post('target')));
								 
								 $banner 	= new Banner();
								 $banner->where('id', $banner_id)
										->where('id >', 0)
										->get(1);
								 if($banner->exists())
								 {
										 $banner->where('id', $banner_id)
												->where('id >', 0)
												->update(array(
													 'name' 	=> $name,
													 'dest' 	=> $dest,
													 'alt' 		=> $alt,
													 'width' 	=> $width,
													 'height' 	=> $height,
													 'target' 	=> $target,
													 'b_updated' 	=> time()
												));
								 }
								 
								 die(json_encode(array(
										'stat'		=>'success',
										'banner_id'	=>$banner_id
								 ) ));
						}
				}				
				
				function ajax_delete_banner() {
						if( $this->input->is_ajax_request() && $this->input->post('banner_id') && $this->session->userdata('logged_in') && $this->session->userdata('uid')>0) 
						{
								$banner = new Banner();
								$banner->where('user_id', (int)$this->session->userdata('uid'))
									   ->where('id', (int)$this->input->post('banner_id'))
									   ->where('id >', 0)
									   ->get(1);
								if($banner->exists())
								{
										$cfg = $this->config->item('datamapper');
										$this->db->delete($cfg['prefix'] . 'banners', array('id' => (int)$this->input->post('banner_id'))); 
								}
								die(json_encode(array('stat'=>'success')));
						}
				}
								
				function manage_text_links() {
						$this->_auth_required();
						
						$opts = read_options('company_name', 'OBJECT');
						
						$data = array();
						$data['base_url'] 		= base_url();
						$data['company_name'] 	= $opts->company_name;
						
						$data['links'] = new Text_link();
						if($this->session->userdata('logged_in') && $this->session->userdata('uid')>0)
						{
								$data['links']->where('user_id', (int)$this->session->userdata('uid'))
											  ->order_by('id', 'desc')
											  ->get();
						}
						
						$this->load->library('template');
						$this->template->set_template('account');
						$this->template->write('title', $data['company_name'] . ' &middot; ' . 'Trader Room &gt; Manage Text Links');
						$this->template->write('base_url', base_url());
						$this->template->write('csrf', $this->security->get_csrf_hash());
						$this->template->write_view('header', 'layouts/account/header', array('base_url'=>base_url()));
						$this->template->write_view('sidebar', 'layouts/account/sidebar', array('base_url'=>base_url(), 'active_menus'=>array('affiliate')));
						$this->template->write_view('content', 'account/manage_text_links', $data);
						$this->template->write_view('footer', 'layouts/account/footer');
						$this->template->render();
				}
				
				function add_text_link() {
						$this->_auth_required();
						
						$opts = read_options('company_name', 'OBJECT');
						
						$data = array();
						$data['base_url'] 		= base_url();
						$data['company_name'] 	=$opts->company_name;
												
						$this->load->library('template');
						$this->template->set_template('account');
						$this->template->write('title', $data['company_name'] . ' &middot; ' . 'Trader Room &gt; Add Text Link');
						$this->template->write('base_url', base_url());
						$this->template->write('csrf', $this->security->get_csrf_hash());
						$this->template->write_view('header', 'layouts/account/header', array('base_url'=>base_url()));
						$this->template->write_view('sidebar', 'layouts/account/sidebar', array('base_url'=>base_url(), 'active_menus'=>array('affiliate')));
						$this->template->write_view('content', 'account/add_text_link', $data);
						$this->template->write_view('footer', 'layouts/account/footer');
						$this->template->render();
				}
				
				function edit_text_link() {
						$this->_auth_required();
						
						$uri = $this->uri->uri_to_assoc(3);
						if(!isset($uri['id'])) redirect(base_url() . 'account/manage_text_links');
						
						$data 			= array();
						$link_id 		= (int)$uri['id'];
						$data['link']   = new Text_link();
						$data['link']->where('user_id', (int)$this->session->userdata('uid'))
									 ->where('user_id >', 0)
									 ->where('id', $link_id)
									 ->where('id >', 0)
									 ->get(1);
						if(!$data['link']->exists()) redirect(base_url() . 'account/manage_text_links');
						
						$opts = read_options('company_name', 'OBJECT');
						
						$data['base_url'] 		= base_url();
						$data['company_name'] 	= $opts->company_name;
												
						$this->load->library('template');
						$this->template->set_template('account');
						$this->template->write('title', $data['company_name'] . ' &middot; ' . 'Trader Room &gt; Edit Link'.($data['link']->name?' &gt; ' . $data['link']->name:''));
						$this->template->write('base_url', base_url());
						$this->template->write('csrf', $this->security->get_csrf_hash());
						$this->template->write_view('header', 'layouts/account/header', array('base_url'=>base_url()));
						$this->template->write_view('sidebar', 'layouts/account/sidebar', array('base_url'=>base_url(), 'active_menus'=>array('affiliate')));
						$this->template->write_view('content', 'account/edit_text_link', $data);
						$this->template->write_view('footer', 'layouts/account/footer');
						$this->template->render();
				}	
				
				function embed_text_link() {
						$this->_auth_required();
						
						$opts = read_options('company_name', 'OBJECT');
						
						$data = array();
						$data['base_url'] 		= base_url();
						$data['company_name'] 	= $opts->company_name;
						
						$data['links'] = new Text_link();
						$data['links']->order_by('id', 'desc')
									  ->get();
												
						$this->load->library('template');
						$this->template->set_template('account');
						$this->template->write('title', $data['company_name'] . ' &middot; ' . 'Trader Room &gt; Embed Text Link');
						$this->template->write('base_url', base_url());
						$this->template->write('csrf', $this->security->get_csrf_hash());
						$this->template->write_view('header', 'layouts/account/header', array('base_url'=>base_url()));
						$this->template->write_view('sidebar', 'layouts/account/sidebar', array('base_url'=>base_url(), 'active_menus'=>array('affiliate')));
						$this->template->write_view('content', 'account/embed_text_link', $data);
						$this->template->write_view('footer', 'layouts/account/footer');
						$this->template->render();
				}
				
				function ajax_add_text_link() {
						if( $this->input->is_ajax_request() && $this->input->post('name') && $this->input->post('text') && $this->input->post('dest') ) 
						{
								 $name 		= trim($this->security->xss_clean($this->input->post('name')));
								 $text 		= trim($this->security->xss_clean($this->input->post('text')));
								 $dest 		= trim($this->security->xss_clean($this->input->post('dest')));
								 $alt 		= trim($this->security->xss_clean($this->input->post('alt')));
								 $target 	= trim($this->security->xss_clean($this->input->post('target')));
								 
								 if($this->session->userdata('logged_in') && $this->session->userdata('uid')>0)
								 {
										 $text_link 			= new Text_link();
										 $text_link->user_id 	= $this->session->userdata('uid');
										 $text_link->name 		= $name;
										 $text_link->text 		= $text;
										 $text_link->dest 		= $dest;
										 $text_link->alt 		= $alt;
										 $text_link->target 	= $target;
										 $text_link->t_added 	= time();
										 $text_link->save();
										 
										 die(json_encode(array(
												'stat'		=>'success',
												'link_id'	=>$this->db->insert_id()
										 ) ));
								 }
						}
				}
				
				function ajax_save_text_link() {
						if( $this->input->is_ajax_request()&& $this->input->post('link_id') && $this->input->post('name') && $this->input->post('text') && $this->input->post('dest')  && $this->session->userdata('logged_in') && $this->session->userdata('uid')>0) 
						{
								 $link_id = (int)$this->input->post('link_id');
								 $name 		= trim($this->security->xss_clean($this->input->post('name')));
								 $text 		= trim($this->security->xss_clean($this->input->post('text')));
								 $dest 		= trim($this->security->xss_clean($this->input->post('dest')));
								 $alt 		= trim($this->security->xss_clean($this->input->post('alt')));
								 $target 	= trim($this->security->xss_clean($this->input->post('target')));
								 
								 $link 	= new Text_link();
								 $link->where('user_id', (int)$this->session->userdata('uid'))
									  ->where('user_id >', 0)
									  ->where('id', $link_id)
									  ->where('id >', 0)
									  ->get(1);
								 if($link->exists())
								 {
										   $link->where('id', $link_id)
												->where('id >', 0)
												->update(array(
													 'name' 	=> $name,
													 'text' 	=> $text,
													 'alt' 		=> $alt,
													 'target' 	=> $target,
													 't_updated' 	=> time()
												));
								 }
								 
								 die(json_encode(array(
										'stat'		=>'success',
										'link_id'	=>$link_id
								 ) ));
						}
				}	
				
				function ajax_delete_text_link() {
						if( $this->input->is_ajax_request() && $this->input->post('link_id') && $this->session->userdata('logged_in') && $this->session->userdata('uid')>0) 
						{
								$link = new Text_link();
								$link->where('user_id', (int)$this->session->userdata('uid'))
									 ->where('user_id >', 0)
									 ->where('id', (int)$this->input->post('link_id'))
									 ->where('id >', 0)
									 ->get(1);
								if($link->exists())
								{
										$cfg = $this->config->item('datamapper');
										$this->db->delete($cfg['prefix'] . 'text_links', array('id' => (int)$this->input->post('link_id'))); 
								}
								die(json_encode(array('stat'=>'success')));
						}
				}
				
				function ajax_get_captcha(){
						if ( $this->input->is_ajax_request() ) {
								$this->load->helper('captcha');
								$vals = array(
									'img_path' => './images/captcha/',
									'img_url' => base_url() . 'images/captcha/',
								);
								$data = array();
								$data['captcha'] = create_captcha($vals);
								$captcha = new Captcha();
								$captcha->captcha_time 	= $data['captcha']['time'];
								$captcha->ip_address 	= $this->input->ip_address();
								$captcha->word 			= $data['captcha']['word'];
								$captcha->save();
								
								$this->_purge_captchas();
						
								die( json_encode(array('stat'=>'success', 'captcha'=>$data['captcha']['image'])) );
						}
				}
				
				function _purge_captchas(){
						$cfg = $this->config->item('datamapper');
						$this->db->where('captcha_time <',time()-7200);
						$this->db->delete($cfg['prefix'] . 'captcha');
				}
				
				function ajax_acct_pass(){						
						if ( $this->input->is_ajax_request() ) {
								$this->_auth_required('ajax',base_url().'account/password_center');								
								if( $this->input->post('type')=='change' ) {
										$this->load->library('encrypt');
										if($this->input->post('cur_pass') && $this->input->post('new_pass') && $this->input->post('new_pass2') && $this->input->post('new_pass')==$this->input->post('new_pass2')) {
												$user = new User();
												//$user->where('id',(int)$this->session->userdata('uid'))->where('password',$this->encrypt->encode($this->security->xss_clean($this->input->post('cur_pass'))))->get(1);
												$user->where('id',(int)$this->session->userdata('uid'))->get(1);
										
												if( !$user->exists() )
												{
														die( json_encode(array('stat'=>'error', 'field_id'=>'cur_pass','msg'=>'Your account was not found in database. Please contact admin!')) );
												}
												elseif($this->security->xss_clean($this->input->post('cur_pass'))!=$this->encrypt->decode($user->password))
												{
														die( json_encode(array('stat'=>'error', 'field_id'=>'cur_pass','msg'=>'Does not match your current password!')) );
												}
												else
												{
														$tmp_pass = new Tmp_pass();
														$token = random_string('unique');
														$tmp_pass->where('token',$token)->get(1);
														while($tmp_pass->exists()) {
																$tmp_pass->clear();
																$token = random_string('unique');
																$tmp_pass->where('token',$token)->get(1);
														}
														$tmp_pass->user_id = $this->session->userdata('uid');
														$tmp_pass->type = 'change';
														$tmp_pass->object_str = $this->input->post('object');
														$tmp_pass->token = $token;
														$tmp_pass->pass = $this->encrypt->encode($this->input->post('new_pass'));
														$tmp_pass->added = time();
														$tmp_pass->save();
														
														$this->_email_acct_pass($this->session->userdata('uid'),$tmp_pass->type,$tmp_pass->object_str,$tmp_pass->token);
														
														die( json_encode(array('stat'=>'success', 'msg'=>'A confirmation link was sent to your e-mail. Please follow it to complete!')) );
												}
										}
										else 
										{
												die("missing params");
										}
								}
								elseif( $this->input->post('type')=='restore' ) {
										if($this->input->post('captcha')) {
												$captcha = new Captcha();
												$captcha->where('UCASE(word)',strtoupper($this->security->xss_clean($this->input->post('captcha'))))
														->where('ip_address',$this->input->ip_address())
														->where('captcha_time >',time()-7200)
														->get(1);
												if( !$captcha->exists() )
												{
														die( json_encode(array('stat'=>'error', 'field_id'=>'captcha','msg'=>'Incorrect word!')) );
												}
												else
												{
														$captcha->delete();
														$user = new User();
														$pass = '';
														if($this->input->post('object')=='la')
														{
																$user->where('id',(int)$this->session->userdata('uid'))->get(1);
																if($user->exists())
																{
																		$pass = $this->encrypt->decode($user->password);
																}
														}
														elseif($this->input->post('object')=='phonepassword')
														{
														
														}
														elseif($this->input->post('object')=='account')
														{
														
														}
														
														if($pass)
														{
																$object_str = "";
																$type_str 	= "";
																if($this->input->post('object')=='phonepassword') 		$object_str = "Phone password";
																elseif($this->input->post('object')=='la') 				$object_str = "Web account password";
																elseif($this->input->post('object')=='account') 		$object_str = "Account {$user->mt4_uid} trader's password";

																$opts = read_options('', 'OBJECT');
																
																$to 		= $user->email;
																$subject 	= "$object_str restore";
																$message 	= "
Hello,
																
You've just requested your $object_str below:
																
$pass
																
Regards,
																
".$opts->company_name."'s Team
";
																
																	$this->load->library('email');
																	$config['protocol'] 	= 'smtp';
																	$config['smtp_host'] 	= ($opts->smtp_ssl=='yes'&& substr(strtolower($opts->smtp_host), 0, 6)!='ssl://' ? 'ssl://' : '') . $opts->smtp_host;
																	$config['smtp_user'] 	= $opts->smtp_user;
																	$config['smtp_pass'] 	= $opts->smtp_pass;
																	$config['smtp_port'] 	= $opts->smtp_port;
																	$config['mailtype'] 	= 'html';
																	$config['charset'] 		= 'utf-8';
																	$config['wordwrap']  	= TRUE;
																	$this->email->initialize($config);
																	$this->email->set_newline("\r\n");
																
																$this->email->from($opts->smtp_user);
																$this->email->to($to);
																$this->email->subject($subject);
																$this->email->message(nl2br($message));
																$this->email->send();
														
														}
														
														die( json_encode(array('stat'=>'success', 'msg'=>'An e-mail containing your current password was sent to your e-mail!')) );
												}
										}
								}
						}
						else die("malformed request");
				}
				
				function _email_acct_pass($user_id,$type,$object,$token){
						$user = new User();
						$user->where('id',(int)$user_id)->get(1);
						if($user->exists())
						{
								$object_str = "";
								$type_str 	= "";
								if($object=='phonepassword') 	$object_str = "Phone password";
								elseif($object=='la') 			$object_str = "Web account password";
								elseif($object=='account') 		$object_str = "Account {$user->mt4_uid} trader's password";

								if($type=='change') 			$type_str = "change";
								elseif($type=='restore') 		$type_str = "Restoration";
								
								$opts = read_options('', 'OBJECT'); 
								
								$tpl_email  = new Tpl_email(); $tpl_email->where('type','password_center_email')->get(1);
								$subject	= $this->_map_client_presets($user->id, $tpl_email->subject);
								$message 	= $this->_map_client_presets($user->id, $tpl_email->message);
								
								$find		= array('{password_type}','{password_center_action}','{password_confirm_url}');
								$replaceby  = array($object_str,$type_str,base_url()."account/pass_conf/$token/");
								$subject    = str_replace($find,$replaceby,$subject);
								$message    = str_replace($find,$replaceby,$message);
								
								$this->load->library('email');
								$config['protocol'] 	= 'smtp';
								$config['smtp_host'] 	= ($opts->smtp_ssl=='yes'&& substr(strtolower($opts->smtp_host), 0, 6)!='ssl://' ? 'ssl://' : '') . $opts->smtp_host;
								$config['smtp_user'] 	= $opts->smtp_user;
								$config['smtp_pass'] 	= $opts->smtp_pass;
								$config['smtp_port'] 	= $opts->smtp_port;
								$config['mailtype'] 	= 'html';
								$config['charset'] 		= 'utf-8';
								$config['wordwrap']  	= TRUE;
								$this->email->initialize($config);
								$this->email->set_newline("\r\n");
								
								$this->email->from($opts->smtp_user);
								$this->email->to($user->email);
								$this->email->subject($subject);
								$this->email->message($message);
								$this->email->send();
								
								log_activity('New Password request sent by ' . $this->_get_current_user_name() . ' (MT4 #' . $this->_get_current_user_mt4_uid() . ')', 'user', 'payment_notice', 'system');
						}
				}
				
				function pass_conf() {
						$token = $this->uri->segment(3);
						if( !$token )
						{
								$this->session->set_flashdata('flash_account_password_center', '<div id="flash_account_password_center">Invalid URL!</div><script language="javascript"> setTimeout("jQuery(\'#flash_account_password_center\').hide();",5000); </script>');
								redirect( base_url() . 'account/password_center' );
						}
						
						$tmp_pass = new Tmp_pass();
						$tmp_pass->where('token',$this->security->xss_clean($token))->get(1);
						if( !$tmp_pass->exists() )
						{
								$this->session->set_flashdata('flash_account_password_center', '<div id="flash_account_password_center" style="font-weight:bold;color:#ff0000;">This URL may have expired!</div><script language="javascript"> setTimeout("jQuery(\'#flash_account_password_center\').hide();",5000); </script>');
								redirect( base_url() . 'account/password_center' );
						}
						
						if($tmp_pass->type=='change')
						{
								if($tmp_pass->object_str=='la')
								{
										$user = new User();
										$user->where('id',$tmp_pass->user_id)->get(1);
										if($user->exists())
										{
												$pass = $this->encrypt->decode($tmp_pass->pass);
												if($this->_sync_mt4_pwd($user->mt4_uid, $pass))
												{
														$cfg = $this->config->item('datamapper');
														$user->clear();
														$user->where('id',$tmp_pass->user_id)->update(array('password'=>$this->encrypt->encode($pass)));
														$this->db->where('token',$this->security->xss_clean($token));
														$this->db->delete($cfg['prefix'] . 'tmp_pass');
														$this->session->set_flashdata('flash_account_password_center', '<div id="flash_account_password_center" style="font-weight:bold;color:#0000ff;">Web account password successfully changed!</div><script language="javascript"> setTimeout("jQuery(\'#flash_account_password_center\').hide();",5000); </script>');
														
														log_activity('Password successfully change in account ' . $this->_get_current_user_name() . ' (MT4 #' . $this->_get_current_user_mt4_uid() . ')', 'user', 'payment_notice', 'system');
												}
												else
												{
														$this->session->set_flashdata('flash_account_password_center', '<div id="flash_account_password_center" style="font-weight:bold;color:#ff0000;">Your password was not changed because of an internal error. Contact admin!</div><script language="javascript"> setTimeout("jQuery(\'#flash_account_password_center\').hide();",5000); </script>');
												}
												redirect( base_url() . 'account/password_center' );
										}
								}
								elseif($tmp_pass->object_str=='phonepassword')
								{
										$user = new User();
										$user->where('id',$tmp_pass->user_id)->get(1);
										if($user->exists())
										{
											$this->session->set_flashdata('flash_account_password_center', '<div id="flash_account_password_center" style="font-weight:bold;color:#0000ff;">Phone password successfully changed!</div><script language="javascript"> setTimeout("jQuery(\'#flash_account_password_center\').hide();",5000); </script>');
											redirect( base_url() . 'account/password_center' );
										}
								}
								elseif($tmp_pass->object_str=='account')
								{
										$user = new User();
										$user->where('id',$tmp_pass->user_id)->get(1);
										if($user->exists())
										{
											$this->session->set_flashdata('flash_account_password_center', '<div id="flash_account_password_center" style="font-weight:bold;color:#0000ff;">Account '.$user->mt4_uid.' trader\'s  password successfully changed!</div><script language="javascript"> setTimeout("jQuery(\'#flash_account_password_center\').hide();",5000); </script>');
											redirect( base_url() . 'account/password_center' );
										}
								}
						}
						
						redirect( base_url() . 'account/password_center' );
				}
				
				function _sync_mt4_pwd($login, $pass){
						$this->load->library('mt4');
						$opts = read_options('mt4_host,mt4_port', 'OBJECT');  
						if($this->mt4->connect($opts->mt4_host, (int)$opts->mt4_port)===FALSE)
						{
								return false;
						}
						$params['login'] 	 = $login;
						$params['pass']  	 = $pass;
						$params['investor']  = 0;
						$answerData 		 = $this->mt4->request('changepassword', $params);
						if($answerData===FALSE) return false;
						if(isset($answerData['result']))
						{
								if($answerData['result']<0) return false;
								else return true;
						}
						return false;
				}
				
				function mt4_op() {
						$this->load->library('cmt4datareceiver');
						$opts = read_options('mt4_host,mt4_port', 'OBJECT'); 
						$host 		= $opts->mt4_host; 
						$port 		= (int)$opts->mt4_port;
						$chk 		= $this->cmt4datareceiver->OpenConnection($host, $port);
						if($chk===0) 
						{
								die("Life's Good!");
						}
						else
						{
								die("failed connection to MT4 server");
						}
				}
				
				function _get_current_user_name() {
						$uid = $this->session->userdata('uid');
						if($uid>0)
						{
								$user = new User();
								$user->where('id', (int)$uid)->get(1);
								if($user->exists()) return $user->firstname . ($user->firstname && $user->lastname ? ' ' : '') . $user->lastname;
						}
				}				
				
				function _get_current_user_mt4_uid() {
						$uid = $this->session->userdata('uid');
						if($uid>0)
						{
								$user = new User();
								$user->where('id', (int)$uid)->get(1);
								if($user->exists()) return $user->mt4_uid;
						}
				}
		}

/* End of file account.php */
/* Location: ./system/application/controller/account.php */